package servlet;

import com.db.conn;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


public class uploadFile extends HttpServlet {

	private File file = null;


	/**
	 * Constructor of the object.
	 */
	public uploadFile() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	//添加用户信息
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");  //设置编码
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");
		PrintWriter out2 = response.getWriter();

		String path=request.getSession().getServletContext().getRealPath("/upload");

		//获得磁盘文件条目工厂
		DiskFileItemFactory factory = new DiskFileItemFactory();

		//如果没以下两行设置的话,上传大的文件会占用很多内存，
		//设置暂时存放的存储室,这个存储室可以和最终存储文件的目录不同
		/**
		 * 原理: 它是先存到暂时存储室，然后再真正写到对应目录的硬盘上，
		 * 按理来说当上传一个文件时，其实是上传了两份，第一个是以 .tem 格式的
		 * 然后再将其真正写到对应目录的硬盘上
		 */
		factory.setRepository(file);
		//设置缓存的大小，当上传文件的容量超过该缓存时，直接放到暂时存储室
		factory.setSizeThreshold(1024 * 1024);
		//高水平的API文件上传处理
		ServletFileUpload upload = new ServletFileUpload(factory);
		SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmssSSS");

		String ss = df.format(new Date()).toString();

		String fileName = ss + ".jpg";


		try {
			List<FileItem> list = upload.parseRequest(request);
			FileItem picture = null;
			for (FileItem item : list) {
				//获取表单的属性名字
				String name = item.getFieldName();
				//System.out.println(name);
				//如果获取的表单信息是普通的 文本 信息
				if (item.isFormField()) {
					//获取用户具体输入的字符串
					String value = item.getString();
					request.setAttribute(name, value);
				} else {
					picture = item;
				}
			}

			String destPath = path +"/"+ fileName;

			//真正写到磁盘上
			File file = new File(destPath);
			OutputStream out = new FileOutputStream(file);
			InputStream in = picture.getInputStream();
			int length = 0;
			byte[] buf = new byte[1024];
			// in.read(buf) 每次读到的数据存放在buf 数组中
			while ((length = in.read(buf)) != -1) {
				//在buf数组中取出数据写到（输出流）磁盘上
				out.write(buf, 0, length);
			}

		System.out.println(fileName);
			in.close();
			out.close();
		} catch (FileUploadException e1) {
		} catch (Exception e) {

			System.out.println(e);
		}

		JSONArray jsonarray = new JSONArray();
		JSONObject jsonobj = new JSONObject();
		jsonobj.put("addr", "/upload/"+fileName);
		jsonarray.add(jsonobj);
		out2 = response.getWriter();
		out2.println(jsonarray);

		out2.flush();
		out2.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

